package com.bawei.module_message.ui


import android.widget.Toast
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.lib_common.Router
import com.bawei.module_message.R
import com.bawei.module_message.adapter.MessageAdapter
import com.bawei.module_message.adapter.MessageExpireAdapter
import com.bawei.module_message.adapter.MessageHandleAdapter
import com.bawei.module_message.databinding.FragmentMessageBinding
import com.bawei.module_message.model.MessageEvent
import com.bawei.module_message.model.MessageModelItem
import com.bawei.module_message.model.RequestEvent
import com.bawei.myclub.base.mvvm.view.BaseFragment
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
@Route(path = Router.Ui.MessageFragmenmt)
class MessageFragment : BaseFragment<MessageViewModel,FragmentMessageBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.fragment_message
    override val mViewModel: MessageViewModel by viewModels()
    private val mMessageAdapter by lazy { MessageAdapter() }
    private val mMessageExpireAdapter by lazy { MessageExpireAdapter() }
    private val mMessageHandleAdapter by lazy { MessageHandleAdapter() }

    override fun initView() {
        mBinding.rv.apply {
            adapter = mMessageAdapter
            layoutManager = LinearLayoutManager(requireContext())
            addItemDecoration(DividerItemDecoration(requireContext(),DividerItemDecoration.VERTICAL))
        }
        mBinding.rv1.apply {
            adapter = mMessageExpireAdapter
            layoutManager = LinearLayoutManager(requireContext())
            addItemDecoration(DividerItemDecoration(requireContext(),DividerItemDecoration.VERTICAL))
        }
        mBinding.rv2.apply {
            adapter = mMessageHandleAdapter
            layoutManager = LinearLayoutManager(requireContext())
            addItemDecoration(DividerItemDecoration(requireContext(),DividerItemDecoration.VERTICAL))
        }
    }

    override fun initData() {
        mViewModel.fetchSystemNotice()
        mViewModel.fetchPropertyService()
        mViewModel.fetchParkingManage()
    }

    override fun initListener() {
        // 观察系统通知状态
        lifecycleScope.launch {
            mViewModel.systemNoticeState.collect { event ->
                when (event) {
                    is MessageEvent.SystemNotice -> {
                        handleState(event.state, mMessageAdapter)
                    }
                    else -> Unit // 其他类型事件无需处理
                }
            }
        }

        // 观察物业服务状态
        lifecycleScope.launch {
            mViewModel.propertyServiceState.collect { event ->
                when (event) {
                    is MessageEvent.PropertyService -> {
                        handleState(event.state, mMessageExpireAdapter)
                    }
                    else -> Unit
                }
            }
        }

        // 观察车位管理状态
        lifecycleScope.launch {
            mViewModel.parkingManageState.collect { event ->
                when (event) {
                    is MessageEvent.ParkingManage -> {
                        handleState(event.state, mMessageHandleAdapter)
                    }
                    else -> Unit
                }
            }
        }

    }

    // 通用状态处理（加载、失败）
    private fun handleState(
        state: RequestEvent<List<MessageModelItem>>,
        adapter: RecyclerView.Adapter<*>
    ) {
        when (state) {
            is RequestEvent.Loading -> {
                // 显示加载动画（如果有）
            }
            is RequestEvent.Success -> {
                val fullList = state.data
                val singleItemList = listOf(fullList.first())
                when (adapter) {
                    is MessageAdapter -> adapter.submitList(singleItemList)
                    is MessageExpireAdapter -> adapter.submitList(singleItemList)
                    is MessageHandleAdapter -> adapter.submitList(singleItemList)
                }
            }
            is RequestEvent.Failed -> {
                Toast.makeText(requireContext(), state.message, Toast.LENGTH_SHORT).show()
            }
        }
    }

}